package com.java.actual.snake;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class SnakeFrame extends JFrame implements ActionListener {

	private StagePanel panel;

	private SnakeMenu snakeMenu;

	private Snake snake;

	private Thread t;
	
	public SnakeFrame() {
		super("贪吃蛇 v2.0");
		snake = new Snake(this);
		panel = new StagePanel(snake);
		snakeMenu = new SnakeMenu(this);
		this.add(panel, BorderLayout.CENTER);
		Runnable snakeRun = new SnakeRunnable(snake, panel);
		this.t = new Thread(snakeRun);
	}

	public void addEventHandler() {
		for (int i = 0; i < 9; i++) {
			snakeMenu.menuItem[i].addActionListener(this);
		}

		//键盘监听
		this.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_UP && snake.getFlag() != Snake.DOWN) {
					snake.setFlag(Snake.UP);
				}
				if (e.getKeyCode() == KeyEvent.VK_DOWN && snake.getFlag() != Snake.UP) {
					snake.setFlag(Snake.DOWN);
				}
				if (e.getKeyCode() == KeyEvent.VK_LEFT && snake.getFlag() != Snake.RIGTH) {
					snake.setFlag(Snake.LEFT);
				}
				if (e.getKeyCode() == KeyEvent.VK_RIGHT && snake.getFlag() != Snake.LEFT) {
					snake.setFlag(Snake.RIGTH);
				}
			}
		});
	}

	public void showMe() {
		addEventHandler();
		setSize(430, 280);
		setVisible(true);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		new SnakeFrame().showMe();
	}

	@SuppressWarnings("static-access")
	public void actionPerformed(ActionEvent e) {
		
		if (e.getActionCommand().equals("Start NewGame")) {
			snakeMenu.menuItem[0].setEnabled(false);
			t.start();
		}

		if (e.getActionCommand().equals("Exit")) {
			System.exit(0);
		}

		if (e.getActionCommand().equals("Slow")) {
			snake.setSpeed(700);
		}

		if (e.getActionCommand().equals("Normal")) {
			snake.setSpeed(500);
		}

		if (e.getActionCommand().equals("Fast")) {
			snake.setSpeed(100);
		}

		if (e.getActionCommand().equals("Custom")) {
			int speed = 500;
			try {
				speed = Integer.parseInt(JOptionPane
						.showInputDialog("请输入出你所要的速度(0-1000)"));
			} catch (Exception e1) {

			}
			snake.setSpeed(speed);
		}

		if (e.getActionCommand().equals("Background...")) {
			JColorChooser jcc = new JColorChooser();
			Color custom;
			custom = jcc.showDialog(this, "Background SelectColor", Color.BLACK);
			panel.setBackground(custom);
		}

		if (e.getActionCommand().equals("Snake...")) {
			JColorChooser jcc = new JColorChooser();
			Color custom;
			custom =jcc.showDialog(this, "Snake SelectColor", Color.BLACK);
			snake.setSnakeColor(custom);
		}

		if (e.getActionCommand().equals("Food...")) {
			JColorChooser jcc = new JColorChooser();
			Color custom;
			custom =jcc.showDialog(this, "Food SelectColor", Color.BLACK);
			snake.setFoodColor(custom);
		}
		panel.repaint();
	}

}
